package com.retailshop.shopuser.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopuser.common.InterfaceVer;
import com.retailshop.shopuser.common.MessageConstants;
import com.retailshop.shopuser.common.Response;
import com.retailshop.shopuser.entity.Admin;
import com.retailshop.shopuser.entity.UserNo;
import com.retailshop.shopuser.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping(value = "/Admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 读取数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Self", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Self(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = "";
            try {
                res = map.get("parm").toString();
            }catch (Exception ex){

            }
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
//            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                Admin admin1 = JSON.parseObject(res, new TypeReference<Admin>() {
                });

                System.out.println(admin1.getUs());

                Admin admin = adminService.selectLogin(admin1.getUs(),admin1.getPwd());
                if (admin != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, admin, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误2", request);
                }
//            } catch (Exception ex) {
//                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
//            }
        }
    }

}
